﻿using NAnt.Core.Attributes;
using System.IO;
using NAnt.Core.Types;
using NAnt.Core;
using NAnt.SVN.Tasks.Constants;
using NAnt.SVN.Attributes;
using NAnt.SVN.Process;

namespace NAnt.SVN.Tasks
{
    /// <summary>
    /// Implementation of the unlock command.
    /// You can run this tasc with the svnpropset tag in NAnt.
    /// $Id$
    /// </summary>
    [TaskName("svnpropset")]
    public class PropSet : SVNFilesetTask
    {
        #region Private Fields

        bool force = false;
        bool recursive = false;
        private string property = string.Empty;
        private string value = string.Empty;

        #endregion

        #region Public fields

        /// <summary>
        /// Force it or not.
        /// </summary>
        [TaskAttribute("force", Required = false)]
        [BooleanValidator()]
        public virtual bool Force
        {
            get { return force; }
            set { force = value; }
        }

        /// <summary>
        /// Force it or not.
        /// </summary>
        [TaskAttribute("recursive", Required = false)]
        [BooleanValidator()]
        public virtual bool Recursive
        {
            get { return recursive; }
            set { recursive = value; }
        }

        /// <summary>
        /// The property value
        /// </summary>
        [TaskAttribute("property",Required=true)]
        [PropertyValidator()]
        public virtual string Property
        {
            get { return property; }
            set { property = value; }
        }

        /// <summary>
        /// The property value
        /// </summary>
        [TaskAttribute("value", Required = true)]
        public virtual string Value
        {
            get { return value; }
            set { this.value = value; }
        }

        /// <summary>
        /// The file to set the properties to
        /// </summary>
        [TaskAttribute("file",Required=false)]
        public virtual FileInfo File
        {
            get;
            set;
        }

        #endregion

        /// <summary>
        /// Initialize the task
        /// </summary>
        protected override void InitializeSVNTask()
        {
            //Set the command
            Command = SVNCommands.PROPSET;
            //Check the conditions
            if (Files == null && File == null)
            {
                throw new BuildException(string.Format(getString("SVN0305"),"File, Fileset"), Location);
            }
            //Fileset overrules the file
            else if (Files == null && File != null)
            {
                Files = new FileSet();
                Files.BaseDirectory = new DirectoryInfo(Project.BaseDirectory);
                Files.FileNames.Add(File.FullName);
            }
            //Prepare the command
            Options.Add(SVNProperties.PrefixProperty(Property));
            Options.Add(quote(Value));
            Options.Add("{0}");
            //Add flags
            if (Force) Options.Add(SVNFlags.FORCE);
            if (Recursive) Options.Add(SVNFlags.RECURSIVE);
        }

        public override void OnHasRunFile(ProcessRunnerData e)
        {
            //
        }
    }
}
